package com.example.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author Shanks
 * @date 2020-05-29
 */
@Slf4j
@Component
public class PrintJob implements SimpleJob {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public void execute(ShardingContext shardingContext) {
        log.info("Thread ID: {}, 任务分片数: {}, 当前分片项: {}, 当前参数: {}, 当前任务名称: {}, 当前任务参数: {}",
                Thread.currentThread().getId(),
                shardingContext.getShardingTotalCount(),
                shardingContext.getShardingItem(),
                shardingContext.getShardingParameter(),
                shardingContext.getJobName(),
                shardingContext.getJobParameter());
        execute();
    }

    private void execute() {
        System.out.println(String.format("当前时间: %s, 定时任务执行中...", DATE_TIME_FORMATTER.format(LocalDateTime.now())));
    }
}